package container

import (
	"fyne.io/fyne/v2"
)

var _ fyne.Layout = (*sizeLayout)(nil)

type sizeLayout struct {
	Size fyne.Size
	Max  bool
}

func newSizeLayout(size fyne.Size) fyne.Layout {
	return &sizeLayout{size, false}
}
func newSizeMaxLayout(size fyne.Size) fyne.Layout {
	return &sizeLayout{size, true}
}
func (g *sizeLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
	for _, child := range objects {
		if !child.Visible() {
			continue
		}
		if g.Max {
			child.Resize(size)
			child.Move(fyne.NewPos(0, 0))
		} else {
			childMin := child.MinSize()
			child.Resize(childMin)
			child.Move(fyne.NewPos(float32(size.Width-childMin.Width)/2, float32(size.Height-childMin.Height)/2))
		}
	}
}

func (g *sizeLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
	minSize := fyne.NewSize(0, 0)
	for _, child := range objects {
		if !child.Visible() {
			continue
		}

		minSize = minSize.Max(child.MinSize())
	}
	minSize = minSize.Max(g.Size)

	return minSize
}
